package com.jxc.controller;

import com.github.pagehelper.PageInfo;
import com.jxc.UI.EasyUIData;
import com.jxc.VO.ResponceVO;
import com.jxc.VO.UserVO;
import com.jxc.log.JXCLog;
import com.jxc.pojo.Role;
import com.jxc.pojo.User;
import com.jxc.pojo.UserRole;
import com.jxc.service.LogService;
import com.jxc.service.RoleService;
import com.jxc.service.UserRoleService;
import com.jxc.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/admin/user")
public class UserAdminController {

    @Resource
    private UserService userService;
    @Resource
    private RoleService roleService;
    @Resource
    private LogService logService;
    @Resource
    private UserRoleService userRoleService;

    /**
     * 查询用户列表
     * @param
     * @param page
     * @param rows
     * @return
     * @throws Exception
     */
    @GetMapping
    @JXCLog(type = JXCLog.Logtype.Query,operation = "查询用户列表1111")
    public EasyUIData list(String userName, @RequestParam(value="page",required=false,defaultValue = "1")Integer page, @RequestParam(value="rows",required=false,defaultValue = "10")Integer rows)throws Exception{
        PageInfo<UserVO> pageInfo = userService.selectByExample(userName,page,rows);
        for (UserVO u:pageInfo.getList()
             ) {
            StringBuffer sb = new StringBuffer();
            List<Role> roles = roleService.findRolesById(u.getId());
            for (Role r:roles
                 ) {
                sb.append(r.getName()+",");
            }
            //String sb2 = sb.toString().replaceFirst(",","");
            String sb2 = sb.toString();
            if(sb2.length() >= 1){
                String sb3 = sb2.substring(0,sb2.length()-1);
                u.setRoles(sb3);
            }


        }
        long total = pageInfo.getTotal();
        List<UserVO> list = pageInfo.getList();
        EasyUIData easyUIData = new EasyUIData();
        easyUIData.setRows(list);
        easyUIData.setTotal(total);
        return  easyUIData;
    }

    /**
     * 添加和修改用户
     * @param user
     * @return
     */
    @PostMapping
    @JXCLog(operation = "新增用户1111",type = JXCLog.Logtype.Add)
    public ResponceVO saveUserAdmin(User user){
        /**
         * 你在后台接收传值的时候转码：String content=new String(传值.getBytes("ISO-8859-1"),"utf-8");
         * HttpServletRequest req = ServletActionContext.getRequest();
         * req.setCharacterEncoding("UTF-8");
         */
        ResponceVO responceVO = new ResponceVO();
        if(user.getId() != null){
            //修改
            responceVO.setSuccess(true);
            userService.updateByPrimaryKeySelective(user);
            return responceVO;
        }else{
            //添加
            if(userService.findUsersName(user.getUserName()).size() == 0){
                responceVO.setSuccess(true);
                userService.saveinsert(user);
                return  responceVO;
            }
        }
        responceVO.setError("失败");
        return  responceVO;
    }

    /**
     * 删除用户
     * @param
     * @return
     */
    @DeleteMapping
    public ResponceVO deleteAdminUser(Integer id){
        ResponceVO responceVO = new ResponceVO();
        if(id != null){
            //删除日志表
            logService.delLog(id);
            //删除中间表
            userRoleService.delUserRoles(id);
            //删除用户表
            userService.deleteByPrimaryKey(id);
            responceVO.setSuccess(true);
            return  responceVO;
        }
        responceVO.setError("错误！！！");
        return  responceVO;
    }

    /**
     * 角色分配
     * @param
     * @return
     */
    @RequestMapping("/saveRoleSet")
    public ResponceVO updRole(String roleIds,Integer userId){
        ResponceVO responceVO = new ResponceVO();
        if(roleIds != null && userId != null){
            UserRole userRole = new UserRole();
            //删除中间表
            userRoleService.delUserRoles(userId);
            String[] split = roleIds.split(",");
            for (String rid: split
                 ) {
                userRole.setUserId(userId);
                userRole.setRoleId(Integer.parseInt(rid));
                //添加中间表
                userRoleService.insertSelective(userRole);
            }
            responceVO.setSuccess(true);
        }

        return  responceVO;
    }
}
